###############################################
# Dynamic DiD: Visas Haiti and Crime Rates
###############################################

rm(list=ls())

library(Hmisc)
library(did)
library(readstata13)
library(foreign)
library(tidyverse)
library(ggplot2)
set.seed(1111)

#sink("08_results_rates_haiti.txt")

############################
# Read and prepare data 
############################

# load data
load("final_county_2023april6.RData")
names(d_county)

#######################
# Results 
#######################

# d-did
out7 <- att_gt(yname = "crime_rates_s",
               gname = "first.treat_haiti",
               idname = "county",
               tname = "survey",
               data = d_county,
               est_method = "reg"
)

es7 <- aggte(out7, type = "dynamic",na.rm = TRUE)
summary(es7)
ggdid(es7)

# save results for overall effect
pe7 <- es7$overall.att
se7 <- es7$overall.se
results7 <- rbind(pe7,se7) 
results7
write.table(results7, "overallATT_7.txt", sep="\t")

# table formal to prepare plot
event_study_diff_never <-   data.frame(
  type          = "dynamic",
  term = paste0('ATT(', es7$egt, ")"),
  event.time= es7$egt,
  estimate  = es7$att.egt,
  std.error = es7$se.egt,
  conf.low  = es7$att.egt - es7$crit.val.egt * es7$se.egt,
  conf.high = es7$att.egt + es7$crit.val.egt  * es7$se.egt)

# plot
p_es_never4 <- ggplot(data = event_study_diff_never,
                      mapping = aes(x = event.time, y = estimate)) +
  geom_line(size = 0.5, alpha = 2, colour = "black") +
  theme_light()  +
  geom_vline(xintercept = 0-0.05, color = 'grey', size = 1.2, linetype = "dotted") + 
  geom_hline(yintercept = 0, colour="black",  linetype = "dotted")+
  geom_pointrange(aes(ymin = conf.low, ymax = conf.high), show.legend = FALSE, linetype= 1, size = 1.1,
                  color=ifelse(event_study_diff_never$event.time<0,"gray60","black"))+
  xlab("Survey wave since immigration shock") +
  ylab("DiD Estimate (in standard deviation units)") +
  scale_x_continuous(breaks = c(-7:8)) +
  scale_y_continuous(limits = c(-4,4)) +
  theme(axis.text.y = element_text(size = 14),axis.title.y = element_text(size = 18)) +
  theme(axis.text.x = element_text(size = 14),axis.title.x = element_text(size = 18)) +
  theme(axis.title = element_text(color="black",  size = 12))+
  theme(plot.title=ggtext::element_markdown(size = 12,
                                            color="black",
                                            hjust=0,
                                            lineheight=1.2)
  ) +
  annotate(geom="text", x=1.4, y=3.5, label="Average of post-treatment coefficients (in standard deviation units):
                                            -0.192 
                                            (0.105)",
           color="black",
           size = 7)

p_es_never4

# figure 2 haiti
tiff('figure2_haiti.tiff', units="in", width=10, height=7, res=200, compression = 'lzw')
p_es_never4
dev.off()

# table a16
sink(file = "tableA16.txt")
summary(es7)
sink(file = NULL)

sink()
